//
//  NumberChecker.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/1.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class NumberChecker: NSObject {
    
    public class func checkAlipayAccount(phone : String?)->Bool{
        if phone == nil {
            return false
        }
        let mail = isMail(str: phone)
        if mail == false {
            let predicate = "^1[3,4,5,7,8][0-9][0-9]{8}$"
            let reg = NSPredicate.init(format: "self MATCHES %@", predicate)
            return reg.evaluate(with: phone)
        } else {
            return true
        }
    }
    
    public class func phoneNumCheck(phone : String?) -> Bool {
        guard phone != nil else {
            return false
        }
        if phone!.count >= 6 {
            return true
        }
        return false
//        let predicate = "^1[3,4,5,7,8][0-9][0-9]{8}$"
//        let reg = NSPredicate.init(format: "self MATCHES %@", predicate)
//        return reg.evaluate(with: phone)
    }
    
    public class func isPass(num : String?)->Bool{
        guard num != nil else {
            return false
        }
        if ((num?.count)! > 5) && ((num?.characters.count)! <= 12)  {
            return true
        }
        return false
    }
    
    public class func isMail(str : String?)->Bool{
        guard str?.count != 0 else {
            return false
        }
        let st = "[A-Z0-9a-z_%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        do {
            let expression = try NSRegularExpression.init(pattern: st, options: .caseInsensitive)
            let result = expression.matches(in: str!, options: .reportProgress, range: NSRange.init(location: 0, length: str!.characters.count))
            if result.count > 0 {
                return true
            }
        } catch {
            return false
        }
        return false
    }
    
    public class func validName(name : String?)->Bool{
        guard name != nil else {
            return false
        }
        if name?.count == 0 {
            return false
        }
        if name!.contains(" ") {
            return false
        }
        return true
    }
    
    public class func numberIsEmpty(num : String?)->Bool{
        if num == nil || num!.characters.count == 0{
            return true
        }
        return false
    }
}
